﻿<UserControl x:Class="SuplexApp.SecurityPrincipalDlg"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
             xmlns:local="clr-namespace:SuplexApp"
             xmlns:cc="clr-namespace:SuplexApp.Controls"
             xmlns:splx="clr-namespace:Suplex.Wpf;assembly=Suplex.Wpf"
             xmlns:splxApi="clr-namespace:Suplex.Forms.ObjectModel.Api;assembly=Suplex.Core"
             Loaded="UserControl_Loaded"
             >
    <UserControl.Resources>
        <DataTemplate x:Key="GroupHierItem">
            <Border BorderThickness="2"
                    BorderBrush="{local:FormattedBinding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type local:SuplexTreeViewItem}}, ValueIfTrue=Gray, ValueIfFalse=Transparent}"
                    Background="{local:FormattedBinding Path=IsEditing, RelativeSource={RelativeSource AncestorType={x:Type local:SuplexTreeViewItem}}, ValueIfTrue=LemonChiffon, ValueIfFalse=Transparent}">
                <StackPanel Orientation="Vertical" MaxWidth="400">
                    <Grid HorizontalAlignment="Stretch">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <StackPanel Grid.Column="0" Orientation="Horizontal">
                            <Image Margin="0,0,4,0" Height="16" Width="16"
                                   Source="{local:FormattedBinding Path=IsEnabled, ValueIfTrue=/SuplexAdmin;component/Resources/Security/users-16x16.png, ValueIfFalse=/SuplexAdmin;component/Resources/Security/users-16x16_disabled.png}"/>
                            <TextBlock Text="{local:FormattedBinding Path=Name, FormatString='\{0\}\\'}" VerticalAlignment="Center"/>
                            <TextBlock Text="{local:FormattedBinding Path=Description}" VerticalAlignment="Center" Margin="0,0,4,0" FontWeight="Bold"/>
                        </StackPanel>
                        <Image Grid.Column="1" Source="/SuplexAdmin;component/Resources/gear_menu.png"
                               Opacity=".35" Margin="10,0,1,0" Height="13" Width="23" VerticalAlignment="Center"
                               Visibility="{local:FormattedBinding Path=IsMouseOnItem, RelativeSource={RelativeSource AncestorType={x:Type local:SuplexTreeViewItem}}, ValueIfTrue=Visible, ValueIfFalse=Hidden}"/>
                    </Grid>
                    <Grid Margin="18,1,0,0"
                          Visibility="Visible">
                        <ListBox ItemsSource="{Binding Path=Users}" />
                    </Grid>
                </StackPanel>
            </Border>
        </DataTemplate>
    </UserControl.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <ToolBarTray>
            <ToolBar>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="Filter: " VerticalAlignment="Center"/>
                    <TextBox Width="150" Name="txtFilter" TextChanged="txtFilter_TextChanged" />
                </StackPanel>
                <Button Style="{DynamicResource ToolBar.ButtonStyleKey}"
                    Click="Refresh_Click" ToolTip="Clear Filter">
                    <Image Source="/SuplexAdmin;component/Resources/Security/refresh_16.png" Height="16" Width="22"/>
                </Button>
            <ToolBar>
            </ToolBar>
                <Button Style="{DynamicResource ToolBar.ButtonStyleKey}"
                    Click="AddNewUser_Click" ToolTip="Create New User">
                    <StackPanel Orientation="Horizontal">
                        <Image Source="/SuplexAdmin;component/Resources/Security/user-16x16.png" Height="16" Width="16"/>
                        <TextBlock Text="New User" Margin="5,0,0,0" VerticalAlignment="Center" />
                    </StackPanel>
                </Button>
                <Button Style="{DynamicResource ToolBar.ButtonStyleKey}" Margin="5,0,0,0"
                    Click="AddNewGroup_Click" ToolTip="Create New Group">
                    <StackPanel Orientation="Horizontal">
                        <Image Source="/SuplexAdmin;component/Resources/Security/users-16x16.png" Height="16" Width="16"/>
                        <TextBlock Text="New Group" Margin="5,0,0,0" VerticalAlignment="Center" />
                    </StackPanel>
                </Button>
                <Button Style="{DynamicResource ToolBar.ButtonStyleKey}" Name="tbbDeleteSecurityPrincipal" Margin="5,0,0,0"
                    Click="DeleteSecurityPrincipal_Click" ToolTip="Delete selected Security Principal"
                    IsEnabled="{local:FormattedBinding ElementName=lvSecurity, Path=HasSelectedItem}">
                    <StackPanel Orientation="Horizontal">
                        <Image Source="{local:FormattedBinding ElementName=tbbDeleteSecurityPrincipal, Path=IsEnabled, ValueIfTrue='/SuplexAdmin;component/Resources/Security/user-del-16.png', ValueIfFalse='/SuplexAdmin;component/Resources/Security/user-del-16_disabled.png'}" Height="16" Width="16"/>
                        <TextBlock Text="Delete Security Principal" Margin="5,0,0,0" VerticalAlignment="Center" />
                    </StackPanel>
                </Button>
            </ToolBar>
        </ToolBarTray>

        <Grid Name="grdSec" Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="475" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="0" />
            </Grid.ColumnDefinitions>
            <Grid.Resources>
                <CollectionViewSource x:Key="SecurityPrincipalsCvs" Source="{Binding Path=SecurityPrincipals}">
                    <CollectionViewSource.GroupDescriptions>
                        <PropertyGroupDescription PropertyName="Source" />
                    </CollectionViewSource.GroupDescriptions>
                    <CollectionViewSource.SortDescriptions>
                        <scm:SortDescription PropertyName="Source" Direction="Descending" />
                        <scm:SortDescription PropertyName="Name" />
                    </CollectionViewSource.SortDescriptions>
                </CollectionViewSource>
            </Grid.Resources>

            <splx:SplxListView Name="lvSecurity" Margin="0,0,3,0"
                               ItemsSource="{Binding Source={StaticResource SecurityPrincipalsCvs}}"
                               SelectionChanged="lvSecurity_SelectionChanged">
                <ListView.GroupStyle>
                    <!--<x:Static Member="GroupStyle.Default"/>-->
                    <GroupStyle>
                        <GroupStyle.ContainerStyle>
                            <Style TargetType="{x:Type GroupItem}">
                                <Setter Property="Margin" Value="0,0,0,5"/>
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="{x:Type GroupItem}">
                                            <Expander IsExpanded="True" BorderThickness="0">
                                                <Expander.Header>
                                                    <StackPanel Orientation="Horizontal">
                                                        <TextBlock Text="{local:FormattedBinding Path=Name, FormatString='\{0\}: '}" Margin="3,0,0,0"/>
                                                        <TextBlock Text="{Binding Path=ItemCount}"/>
                                                    </StackPanel>
                                                </Expander.Header>
                                                <Expander.Content>
                                                    <ItemsPresenter />
                                                </Expander.Content>
                                            </Expander>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </GroupStyle.ContainerStyle>
                    </GroupStyle>
                </ListView.GroupStyle>
                <ListView.View>
                    <GridView AllowsColumnReorder="true">
                        <GridViewColumn Header="Name" Width="Auto" CellTemplate="{DynamicResource securityPrincipalNameIconTemplate}" />
                        <GridViewColumn Header="Description" Width="Auto">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate DataType="{x:Type splxApi:SecurityPrincipalBase}">
                                    <TextBlock Text="{Binding Path=Description}" Foreground="{local:FormattedBinding Path=IsEnabled, ValueIfTrue=Black, ValueIfFalse=Gray}" VerticalAlignment="Center"/>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn Header="Last Logon" Width="Auto">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Path=LastLogon}" Foreground="{local:FormattedBinding Path=IsEnabled, ValueIfTrue=Black, ValueIfFalse=Gray}" VerticalAlignment="Center"/>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <!--<GridViewColumn Header="Source" Width="Auto">
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate DataType="{x:Type splxApi:SecurityPrincipalBase}">
                                            <TextBlock VerticalAlignment="Center"
                                                       Foreground="{local:FormattedBinding Path=IsEnabled, ValueIfTrue=Black, ValueIfFalse=Gray}">
                                                <TextBlock.Text>
                                                    <MultiBinding Converter="{StaticResource securityPrincipalSourceConverter}">
                                                        <Binding Path="IsUserObject"/>
                                                        <Binding Path="IsLocal"/>
                                                    </MultiBinding>                                            
                                                </TextBlock.Text>
                                            </TextBlock>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>-->
                    </GridView>
                </ListView.View>
            </splx:SplxListView>

            <GridSplitter Grid.Column="0" Background="{DynamicResource GlyphBrush}" Width="3" Margin="0,5" />

            <Border Name="SecurityContentPanel" Grid.Column="1"
                    Background="White"
                    BorderBrush="Navy" BorderThickness=".5"/>
        </Grid>

    </Grid>
</UserControl>